package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MenuService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author by Dragon
 * @Classname MenuController
 * @Description TODO
 * @Date 2020/2/23 13:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;
    //查询所有
    @RequestMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        try {
            //查询父类菜单
            PageResult parentMenuPage = menuService.findParentMenu(queryPageBean);

            return parentMenuPage;//new Result(true, MessageConst.GET_MENU_SUCCESS,parentMenuPage);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L,null);//Result(false,MessageConst.GET_MENU_FAIL);
        }
    }
    //父类菜单窗口弹出
    @RequestMapping("/findParentMenuById")
     public Result findParentMenuById(Integer id){
        try {
            Menu menu = menuService.findParentMenuById(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
    //子类窗口弹出
    @RequestMapping("/findChildrenMenuById")
    public Result findChildrenMenuById(Integer id){
        try {
            Menu menu = menuService.findChildrenMenuById(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
    //添加父类
    @RequestMapping("/addParent")
    public Result addParent(@RequestBody Menu parentMenu){
        try {
            Menu menu = menuService.addParent(parentMenu);
            return new Result(true,MessageConst.ACTION_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
    //添加子类菜单
    @RequestMapping("/addChildren")
    public Result addChildren(@RequestBody Menu childrenMenu ){
        try {
            menuService.addChildren(childrenMenu);
            return new Result(true,MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
    //删除父类
    @RequestMapping("/delParentById")
    public Result delParentById(Integer id){
        try {
            menuService.delParentById(id);
            return new Result(true,MessageConst.DELETE_Parent_menu_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_Parent_menu_FAIL);
        }
    }
    //删除子类菜单
    @RequestMapping("/delChildrenById")
    public Result delChildrenById( Integer id){
        try {
            menuService.delChildrenById(id);
            return new Result(true,MessageConst.DELETE_Subclasses_menu_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_Subclasses_menu_FAIL);
        }
    }
    //编辑父类菜单
    @RequestMapping("/editParent")
    public Result editParent(@RequestBody Menu parentMenu){
        try {
            int priorityInDB = menuService.findParentMenuById(parentMenu.getId()).getPriority();
            menuService.editParent(parentMenu,priorityInDB);
            return new Result(true,MessageConst.EDIT_Parent_menu_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_Parent_menu_FAIL);
        }
    }
     //编辑子类菜单
    @RequestMapping("/editChildren")
    public Result editChildren(@RequestBody Menu childrenMenu){
        try {
            Integer priorityInDB = menuService.findParentMenuById(childrenMenu.getPriority()).getPriority();
            menuService.editChildren(childrenMenu,priorityInDB);
            return new Result(true,MessageConst.EDIT_lasses_menu_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_lasses_menu_FAIL);
        }
    }

    @RequestMapping("/getMenu")
    public Result getMenu() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Object principal = authentication.getPrincipal();
            //这里我修改了User的pojo类 实现了UserDetails 静态代理了框架的User 所以发到安全容器里的直接是自定义的User pojo
            //这样做的好处是可以直接在用的时候 从安全容器中拿到带自定义的字段的对象
            List<Menu> menuByUser = menuService.getMenuByUser((User) principal);
            return new Result(true, "请求菜单成功", menuByUser);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "请求菜单失败");
        }
    }

    @RequestMapping("findMenuInfo")
    public Result findMenuInfo(){
        try {
            //调用service中的查询方法
            List<Menu> list = menuService.findMenuInfo();
            //查询成功, 返回查询成功的信息
            return new Result(true, "查询菜单数据成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败, 返回查询失败的信息
            return new Result(false,"查询菜单数据成功");
        }
    }

}